<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Customers
 *
 * @author tuankiet
 */
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');

class Customers extends AppModel {

    public $primaryKey = 'CustId';
    //put your code here
    public $validate = array(
        'CustId' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
            'unique' => array(
                'rule' => 'isUnique',
                'required' => 'create',
                'message' => 'ID is exsit',
            ),
        ),
        'CustName' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'CustFurigana' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'CustAddress' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'CustPhone' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => '電話番号を入力してください',
            ),
        ),
        'Email' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
            'unique' => array(
                'rule' => 'isUnique',
                'required' => 'create',
                'message' => 'Email is exsit',
            ),
        ),
        'Pass' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'パスワードを入力してください',
                'on' => 'create'
            ),
        ),
    );

    public function beforeSave($options = array()) {
        parent::beforeSave($options);
        if (!empty($this->data[$this->alias]['Pass'])) {
            $passwordHasher = new SimplePasswordHasher(array('hashType' => 'md5'));
            $this->data[$this->alias]['Pass'] = $passwordHasher->hash(
                    $this->data[$this->alias]['Pass']
            );
        } else {
            unset($this->data[$this->alias]['Pass']);
        }
        return true;
    }

    public function saveData($data, $id = null) {
        if ($id) {
            $this->id = $id;
        } else {
            $this->create();
        }
        $this->set($data);
        if ($this->validates()) {
            return $this->save();
        }
    }

    public function getData($condition = null, $id = null) {
        if (empty($id)) {
            return $this->find('all', array('conditions' => $condition));
        } else {
            return $this->find('first', array(
                        'conditions' => array_merge(array('Customers.CustId ' => $id))
                            )
            );
        }
    }

    /**
     * 
     */
    public function getListCustomer($is_search) {
        $user = CakeSession::read("Auth.User");
        $condition = array();
        if (!$user['IsAdmin']) {
            $condition['Customers.CustId'] = $user['CustId'];
        }
        if (count($condition) > 0) {
            $data = $this->find('list', array('conditions' => $condition, 'fields' => array('CustId', 'CustName')));
        } else {
            $data = $this->find('list', array('fields' => array('CustId', 'CustName')));
        }
        if ($is_search && !isset($condition['Customers.CustId'])) {
            $data = array_merge(array('0' => 'All'), $data);
        }
        return $data;
    }

    public function getCustByEmail($email) {
        return $this->find('all', array('conditions' => array(
                        'Email' => $email
        )));
    }

}
